package jqgridjavawrapper;

/**
 * @author Isuru Madushanka Weerarathna
 * @contact uisurumadushanka89@gmail.com
 */
public class JQGridGrouping implements JQPropertyMaker {

    private String[] groupFields = null;
    private JQGridSortType[] groupOrders = null;
    private String[] groupTexts = null;
    private boolean[] groupColumnShow = null;
    private boolean[] showGroupSummary = null;

    private boolean showSummaryOnHide = false;
    private boolean sortedInGroups = false;
    private boolean groupsCollapsed = false;
    private String iconPlus = null;
    private String iconMinus = null;

    public StringBuilder generateProperties() {
        StringBuilder sb = new StringBuilder();

        if (groupFields!=null) {
            sb.append("\ngroupField: [");
            for (int i=0;i<groupFields.length;i++) {
                if (i>0) sb.append(',');
                sb.append(formatStr(groupFields[i]));
            }
            sb.append("],");
        }

        if (groupOrders!=null) {
            sb.append("\ngroupOrder: [");
            for (int i=0;i<groupOrders.length;i++) {
                if (i>0) sb.append(',');
                sb.append(formatStr(groupOrders[i].toString()));
            }
            sb.append("],");
        }

        if (groupTexts!=null) {
            sb.append("\ngroupText: [");
            for (int i=0;i<groupTexts.length;i++) {
                if (i>0) sb.append(',');
                sb.append(formatStr(groupTexts[i].toString()));
            }
            sb.append("],");
        }

        if (groupColumnShow!=null) {
            sb.append("\ngroupColumnShow: [");
            for (int i=0;i<groupColumnShow.length;i++) {
                if (i>0) sb.append(',');
                sb.append(formatBool(groupColumnShow[i]));
            }
            sb.append("],");
        }

        if (showGroupSummary!=null) {
            sb.append("\ngroupColumnShow: [");
            for (int i=0;i<showGroupSummary.length;i++) {
                if (i>0) sb.append(',');
                sb.append(formatBool(showGroupSummary[i]));
            }
            sb.append("],");
        }

        if (showSummaryOnHide) sb.append("\ngroupSummary:").append(showSummaryOnHide).append(',');
        if (sortedInGroups) sb.append("\ngroupDataSorted:").append(sortedInGroups).append(',');
        if (groupsCollapsed) sb.append("\ngroupCollapse:").append(groupsCollapsed).append(',');
        if (iconPlus!=null) sb.append("\nplusicon:").append(iconPlus).append(',');
        if (iconMinus!=null) sb.append("\nminusicon:").append(iconMinus).append(',');


        return sb;
    }

    private String formatBool(boolean value) {
        return (value ? "true" : "false");
    }

    private String formatStr(String value) {
        return "'" + value + "'";
    }
}
